package com.zoe.app.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Zoe.Zhang
 * @date 2021/10/24
 * @description
 */
@Component
public class RedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean setEx(String key, String value, Long expireTime) {
        stringRedisTemplate.opsForValue().set(key, value);
        Boolean expire = stringRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
        return expire;
    }

    public boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    public boolean remove(String key) {
        if (exists(key)) {
            return stringRedisTemplate.delete(key);
        }
        return false;
    }
}
